package com.hnkywl.sts.dao.base;

import com.hnkywl.sts.entity.base.Dxpz;
import com.hnkywl.sts.entity.base.Jkpz;
import com.hnkywl.sts.entity.sys.Bm;
import com.hnkywl.sts.util.StringFormat;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.SQLQuery;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

/**
 * 接口配置
 * User: Administrator
 * Date: 13-4-17
 * Time: 下午3:17
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class JkpzDao extends SimpleHibernateTemplate<Jkpz, Long> {
    public Page<Jkpz> findJkpzList(String bm, int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Jkpz> page = new Page<Jkpz>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (bm != null && !bm.isEmpty()) {
            condition.append(" bm like ? ");
            param.add("%" + bm.trim() + "%");
        }
        //==============记录数量===============
        hql = "select count(*) from Jkpz";
        if (condition.length() != 0)
            hql = hql + " where " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Jkpz ";
        if (condition.length() != 0)
            hql = hql + " where " + condition;
        hql = hql + " order by dateCreated desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    public Jkpz findByBm(String bm) {
        List<Jkpz> jkpzs = this.find("from Jkpz where bm = ? ", bm);
        return jkpzs.size() == 0 ? null : jkpzs.get(0);
    }
}
